<template>
    <view :class="{'uni-mask':true,'bg-transparent':true}">
        <view class="count-down">
            <text>{{countDown/1000}}s</text>
        </view>

        <view class="get-count">
            <image :src="hongbao"/>
            <text class="x">X</text>
            <text class="hongbao-count">{{count}}</text>
        </view>
        <view class="rain-container" :style="{'animation-duration':`${limitedTime/1000}s`}">
            <view
                    class="red-rain" v-for="(item, index) in baglist"
                    :key="index"
                    :style="{'margin-top':item.position/100+'rem'}"
            >
                <view @click="onClick(item)" :class="{'red-bag':true,'big':item.big}"></view>
            </view>
        </view>
    </view>
</template>


<script>
    import debounce from 'lodash.debounce'

    export default {
        name: 'MoneyRain',
        props: {
            //抢红包持续时间
            limitedTime: {
                type: Number,
                default: 18000
            },
            //红包个数
            num:{
                type: Number,
                default: 100
            },
            //红包展示高度
            heightSpace: {
                type: Number,
                default: 380
            },
        },
        created() {
            this.init()
        },
        data() {
            return {
                //倒计时
                countDown: this.limitedTime,
                //红包个数
                list:new Array(this.num),
                //抢到个数
                count: 0,
                hongbao: "../../static/img/active-money-rain/hongbao1.png"
            };
        },
        computed: {
            baglist() {
                for (let i = 0; i < this.list.length; i++) {
                    this.list[i] = {position: Math.random() * this.heightSpace}
                }
                return this.list;
            },
        },
        methods: {
            changeHongbao: debounce(function () {
                this.hongbao = "../../static/img/active-money-rain/hongbao1.png";
            }, 500),
            init() {
                this.count = 0;
                this.timer = setInterval(() => {
                    if (this.countDown === 0) {
                        this.timer && clearInterval(this.timer);
                        this.$emit('end', this.count)
                    } else {
                        this.countDown = this.countDown - 1000;
                    }
                }, 1000);
            },
            onClick(item) {
                if (!item.show) {
                    this.hongbao = "../../static/img/active-money-rain/hongbao2.png";
                    this.changeHongbao();
                    item.show = true;
                    this.count++;
                    this.$emit('click', item)
                }
            }
        }
    }
</script>

<style lang="scss">
    @keyframes mymove {
        from {
            transform: translateY(-100%);
        }
        to {
            transform: translateY(0);
        }
    }

    @keyframes coinmove {
        0% {
            top: 0;
        }
        70% {
            top: 0;
        }
        100% {
            top: 50px;
        }
    }

    @keyframes coinroate {
        0% {
            transform: rotateY(0);
        }
        70% {
            transform: rotateY(-180deg);
        }
        100% {
            transform: rotateY(-180deg);
        }
    }


    .uni-mask {
        z-index: 1002;
        /*background: url("../static/img/active-money-rain/bg.png") center bottom no-repeat;*/
        background: linear-gradient(rgba(6, 6, 6, 0.9), rgba(0, 0, 0, .6));
        background-size: cover;
    }

    .rain-container {
        display: flex;
        box-sizing: border-box;
        flex-flow: row wrap;
        position: absolute;
        transform: translateY(-100%);
        top: 0;
        animation-name: mymove;
        animation-timing-function: linear;
        animation-delay: 1s;
    }

    .get-count {
        z-index: 1003;
        position: absolute;
        top: 100px;

        image {
            width: 190px;
            height: 210px;
            vertical-align: bottom
        }

        text {
            color: #fff;
        }

        .x {
            padding: 10px;
            font-size: 36px;
            display: inline-block;
            margin-bottom: 60px;
            margin-left: -40px;
        }

        .hongbao-count {
            font-size: 60px;
            vertical-align: sub;
        }
    }

    .count-down {
        z-index: 1003;
        color: red;
        background: url("../static/img/active-money-rain/time_bg.png") no-repeat;
        font-size: 36px;
        font-weight: bold;
        width: 110px;
        height: 110px;
        background-size: contain;
        text-align: center;
        line-height: 120px;
        position: absolute;
        top: 160px;
        right: 40px;
    }

    .red-rain {
        width: 20%;
        position: relative;
        /*background: blue;*/
        .red-bag {
            /*transform: rotate(20deg);*/
            background: url("../static/img/active-money-rain/red_small.png");
            background-size: cover;
            width: 82px;
            height: 90px;
            padding: 20px;
            display: block;
            margin: auto;
            position: relative;

            /*&.big {*/
            /*    transform: rotate(20deg) scale(1.5);*/
            /*}*/

            text {
                font-size: 16px;
                top: 16px;
                position: absolute;
                text-align: center;
                color: red;
                width: 100%;
                display: none;
            }
        }
    }

    .hide {
        display: none;
    }
</style>

